package me.nereo.multi_image_selector.utils;

import android.content.Context;
import android.graphics.Bitmap;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/***
 * 功能描述:图片 、Bitmap 管理类
 * 作者:
 * 时间:2016/8/9
 * 版本:
 ***/
public class BitmapUtil {


    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
    /**
     * @param  bitmap    原图
     * @param edgeLength  希望得到的正方形部分的边长
     * @return  缩放截取正中部分后的位图。
     */
    public static Bitmap centerSquareScaleBitmap(Bitmap bitmap, int edgeLength) {
        //   Bitmap bitmap=BitmapFactory.decodeFile(filePath);
        if(null == bitmap || edgeLength <= 0) {
            return  null;
        }

        Bitmap result = bitmap;
        int widthOrg = bitmap.getWidth();
        int heightOrg = bitmap.getHeight();

        if(widthOrg > edgeLength && heightOrg > edgeLength) {
            //压缩到一个最小长度是edgeLength的bitmap
            int longerEdge = (edgeLength * Math.max(widthOrg, heightOrg) / Math.min(widthOrg, heightOrg));
            int scaledWidth = widthOrg > heightOrg ? longerEdge : edgeLength;
            int scaledHeight = widthOrg > heightOrg ? edgeLength : longerEdge;
            Bitmap scaledBitmap;

            try{
                scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
            }
            catch(Exception e){
                return null;
            }
            //从图中截取正中间的正方形部分。
            int xTopLeft = (scaledWidth - edgeLength) / 2;
            int yTopLeft = (scaledHeight - edgeLength) / 2;

            try{
                result = Bitmap.createBitmap(scaledBitmap, xTopLeft, yTopLeft, edgeLength, edgeLength);
                scaledBitmap.recycle();
            }
            catch(Exception e){
                return null;
            }
        }

        return result;
    }

    public static String saveBitmapToFile(Context context,Bitmap mBitmap, String fileName, int quality,boolean isPng) {
        File file = new File(context.getFilesDir().getAbsolutePath()+"/image");
        if (!file.exists()){
            file.mkdirs();// 创建文件夹
        }
        fileName = context.getFilesDir().getAbsolutePath()+"/image" + File.separator + fileName;//图片名字
        FileOutputStream b = null;
        try {
            b = new FileOutputStream(fileName);
            if (isPng) {
                mBitmap.compress(Bitmap.CompressFormat.PNG, quality, b);// 把数据写入文件
            }else {
                mBitmap.compress(Bitmap.CompressFormat.JPEG, quality, b);// 把数据写入文件
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (mBitmap != null && !mBitmap.isRecycled()) {
                mBitmap.recycle();  //回收图片所占的内存
                System.gc();  //提醒系统及时回收
            }
            try {
                //关闭流
                b.flush();
                b.close();
                return fileName;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String saveBitmapToFile1(Context context,Bitmap mBitmap, String fileName, int quality,boolean isPng) {
        File file = new File(context.getFilesDir().getAbsolutePath()+"/image");
        if (!file.exists()){
            file.mkdirs();// 创建文件夹
        }
        fileName = context.getFilesDir().getAbsolutePath()+"/image" + File.separator + fileName;//图片名字
        FileOutputStream b = null;
        try {
            b = new FileOutputStream(fileName);
            if (isPng) {
                mBitmap.compress(Bitmap.CompressFormat.PNG, quality, b);// 把数据写入文件
            }else {
                mBitmap.compress(Bitmap.CompressFormat.JPEG, quality, b);// 把数据写入文件
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭流
                b.flush();
                b.close();
                return file.getAbsolutePath();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }


}
